package com.tanhua.server.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tanhua.dubbo.api.BlackListApi;
import com.tanhua.dubbo.api.QuestionApi;
import com.tanhua.dubbo.api.SettingsApi;
import com.tanhua.model.domain.*;
import com.tanhua.model.domain.mysql.Question;
import com.tanhua.model.domain.mysql.Settings;
import com.tanhua.model.domain.mysql.UserInfo;
import com.tanhua.model.domain.vo.SettingsVo;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @author lk
 * @date 2022-03-25
 */
@Service
public class SettingsService {
    @DubboReference
    private SettingsApi settingsApi;
    @DubboReference
    private QuestionApi questionApi;
    @DubboReference
    private BlackListApi blackListApi;

    //查询通用设置(不包括黑名单)
    public SettingsVo settings() {
        SettingsVo vo = new SettingsVo();
        //获取用户id
        Long userId = UserHolder.getUserId();
        vo.setId(userId);
        //获取手机号
        String phone = UserHolder.getMobile();
        vo.setPhone(phone);

        //获取用户的陌生人问题
        Question question = questionApi.findByUserId(userId);
        String txt = question == null ? "为什么我眼里常含泪水?" : question.getTxt();

        //获取用户通知设置的开关数据
        Settings settings = settingsApi.findByUserId(userId);
        //新用户或者从来没设置过 开关会默认全都开启,但是数据库不会记录数据
        if (settings != null){
            vo.setGonggaoNotification(settings.getGonggaoNotification());
            vo.setLikeNotification(settings.getLikeNotification());
            vo.setPinglunNotification(settings.getPinglunNotification());
        }
        return vo;
    }

    //陌生人问题设置
    public void saveQuestion(String content) {
        //获取当前用户ID
        Long userId = UserHolder.getUserId();
        //调用api查询当前用户的陌生人问题
        Question question = questionApi.findByUserId(userId);
        //判断问题是否存在
        if (question ==null){
            //如果不存在，保存
            question = new Question();
            question.setUserId(userId);
            question.setTxt(content);
            questionApi.save(question);
        }else{
            //如果存在则更新
            question.setTxt(content);
            questionApi.update(question);
        }
    }

    //通知设置
    public void saveSettings(Map map) {
        //获取当前用户ID
        Long userId = UserHolder.getUserId();
        //获取设置信息
        boolean likeNotification = (Boolean) map.get("likeNotification");
        boolean pinglunNotification = (Boolean) map.get("pinglunNotification");
        boolean gonggaoNotification = (Boolean)  map.get("gonggaoNotification");
        //根据用户id，查询用户的通知设置
        Settings settings = settingsApi.findByUserId(userId);
        //判断是否为空
        if (settings ==null){
            //为空则保存  需要新创建一个Settings对象
            settings = new Settings();
            //设置保存的参数
            settings.setId(userId);
            settings.setPinglunNotification(pinglunNotification);
            settings.setLikeNotification(likeNotification);
            settings.setGonggaoNotification(gonggaoNotification);
            settingsApi.save(settings);
        }else{
            //不为空则修改
            settings.setPinglunNotification(pinglunNotification);
            settings.setLikeNotification(likeNotification);
            settings.setGonggaoNotification(gonggaoNotification);
            settingsApi.update(settings);
        }
    }

    //分页查询黑名单列表
    public PageResult blacklist(int page, int size) {
        //获取当前用户id
        Long userId = UserHolder.getUserId();
        //调用API查询用户的黑名单分页列表  Ipage对象
        IPage<UserInfo> iPage = blackListApi.findByUserId(userId,page,size);
        //对象转化，将查询的Ipage对象的内容封装到PageResult中
        PageResult pageResult = new PageResult(page,size, (int) iPage.getTotal(),iPage.getRecords());
        return pageResult;
    }

    //取消黑名单
    public void deleteBlackList(Long blackUserId) {
        //获取当前用户id
        Long userId = UserHolder.getUserId();
        blackListApi.delete(blackUserId,userId);
    }
}
